home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: intrepid.cia.com!not-for-mail
- From: actuary@nando.net (Bill McCarthy)
- Subject: Re: File Open Problem - Please provide clues
- Message-ID: <31f7cc$d436.37b@intrepid.cia.com>
- Date: Sun, 31 Mar 1996 20:04:54 GMT
- References: <4jkq8h$1it6@useneta1.news.prodigy.com>
- Reply-To: actuary@nando.net (Bill McCarthy)
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4jkq8h$1it6@useneta1.news.prodigy.com>,
- DPMU12A@prodigy.com (Steve Bailey) writes:
-
- >Hi I'm having problems with opening files which are named
- >after simple numbers:
- >2
- >5, etc.
- >The reason for naming them this way is because I'm writing a
- >program in C that increments a number, and then attempts to
- >open the file that's actually named by that number.
- >I get mismatched type errors when using FOPEN:
- > int num;
- > filepointer = FOPEN(num,"r"); or
- > filepointer = FOPEN("num","r");
-
- Please post minimal complete code. Otherwise, one wonders if
- "filepointer" was ever defined. Was this code placed inside a
- function. Was the stdio.h header included.
-
- There is no FOPEN() function in C. Why not just use fopen()?
-
- The fopen() function takes two arguments, both of which are
- pointers to const char. In fopen( num, "r" ) you are asking that
- an int be treated as a pointer -- what do think the compiler
- will do with that? Your fopen( "num", "r" ) should attempt to
- open a file named num.
-
- Here a very simple program that will create a file named 1 (or
- rewrite an existing file with that name):
-
- #include <stdio.h>
-
- #define NUMSTRSIZE 10
-
- int main( void )
- {
- FILE *fp;
- int num = 1;
- char fn[ NUMSTRSIZE ];
-
- sprintf( fn, "%d", num );
-
- if ( fp = fopen( fn, "w" ) )
- {
- /* write to file here with error checking */
- fclose( fp );
- }
-
- return 0;
- }
-
- Bill McCarthy
- actuary@nando.net
- Wendell, NC USA
-
-
-